package com.atgouwu.scmall.ware.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 多线程: 线程池
 */
//@EnableConfigurationProperties(ThreadPoolConfigProperties.class)  //ThreadPoolConfigProperties类上加了@Component
@Configuration
public class ThreadConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
        return new ThreadPoolExecutor(pool.getCorePoolSize(), //核心线程数(一直存在)
                pool.getMaximumPoolSize(), //最大线程数(控制资源)
                pool.getKeepAliveTime(), //空闲存活时间(释放:最大-核心)
                TimeUnit.SECONDS, //单位
                new LinkedBlockingDeque<>(100000), //阻塞队列(压测数量): 100000+200(application.xml配置)=100200一次最高处理数量
                Executors.defaultThreadFactory(), //线程工厂
                new ThreadPoolExecutor.AbortPolicy()); //超过100200的拒绝策略(默认抛出异常)
    }

}
